#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import sys, datetime, json, logging

from xpy3lib.utils import db_utils as util

from sicost.config import app_config
from xpy3lib.XLogger import XLogger
from xpy3lib.utils.XUtils import XUtils

from sicost.DP0101Job import DP0101Job
from sicost.DP0102Job import DP0102Job
from sicost.JHZKEYJob import JHZKEYJob
from sicost.CA0101Job import CA0101Job
from sicost.UNINFOJob import UNINFOJob

def main():
    status = 0
    try:
        info = XUtils.get_git_info()
    except Exception as e:
        info = {'message': 'no_git'}

    profile = (sys.argv[1].split("=", 1)[-1]) if len(sys.argv) > 1 else 'prod'
    run_immediately = (sys.argv[2].split("=", 1)[-1] == '1') if len(sys.argv) > 2 else False
    job_name = (sys.argv[3].split("=", 1)[-1]) if len(sys.argv) > 3 else 'all'
    # parameters 4 the certain job
    data_type = (sys.argv[4].split("=", 1)[-1]) if len(sys.argv) > 4 else 'D'
    unit = (sys.argv[5].split("=", 1)[-1]) if len(sys.argv) > 5 else 'Q112'
    account = (sys.argv[6].split("=", 1)[-1]) if len(sys.argv) > 6 else '1001'
    cost_center = (sys.argv[7].split("=", 1)[-1]) if len(sys.argv) > 7 else 'MICA'
    account_period_start = (sys.argv[8].split("=", 1)[-1]) if len(sys.argv) > 8 else '20210510220000'
    account_period_end = (sys.argv[9].split("=", 1)[-1]) if len(sys.argv) > 9 else '20210511220000'

    # NOTE 上面的参数由cli方式传递进来。调用方理应通过代码方式传递。例如：
    # profile = 'prod'
    # run_immediately = True
    # job_name = 'all'
    # # parameters 4 the certain job
    # data_type = 'D'
    # unit = 'Q112'
    # account = '1001'
    # cost_center = 'MICA'
    # account_period_start = '20210510220000'
    # account_period_end = '20210511220000'


    # NOTE set the log-level via profile in config.py
    level = logging.ERROR if profile == 'prod' else logging.NOTSET
    config = app_config[profile]
    XLogger.getInstance(p_level=level, p_name_key_word=config.APPLICATION_NAME).logger.info(
        '\n%s' % json.dumps(info, sort_keys=True, indent=4))

    try:
        db_conn_mpp = util.getConnectionDb2(config.DB_HOST_MPP_DB2,
                                            config.DB_PORT_MPP_DB2,
                                            config.DB_DBNAME_MPP_DB2,
                                            config.DB_USER_MPP_DB2,
                                            config.DB_PASSWORD_MPP_DB2)
        XLogger.getInstance().logger.info('connect db_conn_mpp success')

        db_conn_rds = util.getConnectionDb2(config.DB_HOST_RDS_DB2,
                                            config.DB_PORT_RDS_DB2,
                                            config.DB_DBNAME_RDS_DB2,
                                            config.DB_USER_RDS_DB2,
                                            config.DB_PASSWORD_RDS_DB2)
        XLogger.getInstance().logger.info('connect db_conn_rds success')

        db_conn_dbprod7 = util.getConnectionDb2(config.DB_HOST_DBPROD7_DB2,
                                                config.DB_PORT_DBPROD7_DB2,
                                                config.DB_DBNAME_DBPROD7_DB2,
                                                config.DB_USER_DBPROD7_DB2,
                                                config.DB_PASSWORD_DBPROD7_DB2)
        XLogger.getInstance().logger.info('connect db_conn_dbprod7 success')

    except Exception as e:
        db_conn_mpp = None
        db_conn_rds = None
        db_conn_dbprod7 = None
        XLogger.getInstance().logger.critical(str(e))

    if run_immediately:
        if job_name == 'uninfo':
            run_uninfo_job(p_config=config,
                           p_db_conn_mpp=db_conn_mpp,
                           p_db_conn_rds=db_conn_rds,
                           p_db_conn_dbprod7=db_conn_dbprod7,
                           p_account_period_start=account_period_start,
                           p_account_period_end=account_period_end
                           )
        if job_name == 'dp0101':
            run_dp0101_job(p_config=config,
                           p_db_conn_mpp=db_conn_mpp,
                           p_db_conn_rds=db_conn_rds,
                           p_db_conn_dbprod7=db_conn_dbprod7,
                           p_data_type=data_type,
                           p_unit=unit,
                           p_account=account,
                           p_cost_center=cost_center,
                           p_account_period_start=account_period_start,
                           p_account_period_end=account_period_end
                           )

        if job_name == 'dp0102':
            run_dp0102_job(p_config=config,
                           p_db_conn_mpp=db_conn_mpp,
                           p_db_conn_rds=db_conn_rds,
                           p_db_conn_dbprod7=db_conn_dbprod7,
                           p_data_type=data_type,
                           p_unit=unit,
                           p_account=account,
                           p_cost_center=cost_center,
                           p_account_period_start=account_period_start,
                           p_account_period_end=account_period_end
                           )
        if job_name == 'jhzkey':
            run_jhzkey_job(p_config=config,
                           p_db_conn_mpp=db_conn_mpp,
                           p_db_conn_rds=db_conn_rds,
                           p_db_conn_dbprod7=db_conn_dbprod7,
                           p_data_type=data_type,
                           p_unit=unit,
                           p_account=account,
                           p_cost_center=cost_center,
                           p_account_period_start=account_period_start,
                           p_account_period_end=account_period_end
                           )
        if job_name == 'ca0101':
            run_ca0101_job(p_config=config,
                           p_db_conn_mpp=db_conn_mpp,
                           p_db_conn_rds=db_conn_rds,
                           p_db_conn_dbprod7=db_conn_dbprod7,
                           p_data_type=data_type,
                           p_unit=unit,
                           p_account=account,
                           p_cost_center=cost_center,
                           p_account_period_start=account_period_start,
                           p_account_period_end=account_period_end
                           )
        if job_name == 'all':
            run_dp0102_job(p_config=config,
                           p_db_conn_mpp=db_conn_mpp,
                           p_db_conn_rds=db_conn_rds,
                           p_db_conn_dbprod7=db_conn_dbprod7,
                           p_data_type=data_type,
                           p_unit=unit,
                           p_account=account,
                           p_cost_center=cost_center,
                           p_account_period_start=account_period_start,
                           p_account_period_end=account_period_end
                           )
            run_dp0101_job(p_config=config,
                           p_db_conn_mpp=db_conn_mpp,
                           p_db_conn_rds=db_conn_rds,
                           p_db_conn_dbprod7=db_conn_dbprod7,
                           p_data_type=data_type,
                           p_unit=unit,
                           p_account=account,
                           p_cost_center=cost_center,
                           p_account_period_start=account_period_start,
                           p_account_period_end=account_period_end
                           )
            run_jhzkey_job(p_config=config,
                           p_db_conn_mpp=db_conn_mpp,
                           p_db_conn_rds=db_conn_rds,
                           p_db_conn_dbprod7=db_conn_dbprod7,
                           p_data_type=data_type,
                           p_unit=unit,
                           p_account=account,
                           p_cost_center=cost_center,
                           p_account_period_start=account_period_start,
                           p_account_period_end=account_period_end
                           )
            run_ca0101_job(p_config=config,
                           p_db_conn_mpp=db_conn_mpp,
                           p_db_conn_rds=db_conn_rds,
                           p_db_conn_dbprod7=db_conn_dbprod7,
                           p_data_type=data_type,
                           p_unit=unit,
                           p_account=account,
                           p_cost_center=cost_center,
                           p_account_period_start=account_period_start,
                           p_account_period_end=account_period_end
                           )
    try:
        util.closeConnection(db_conn_mpp)
        util.closeConnection(db_conn_rds)
        util.closeConnection(db_conn_dbprod7)
    except Exception as e:
        XLogger.getInstance().logger.error(str(e))
        status = 1

    return status
def run_uninfo_job(p_config=None,
                   p_db_conn_mpp=None,
                   p_db_conn_rds=None,
                   p_db_conn_dbprod7=None,
                   p_account_period_start=None,
                   p_account_period_end=None):
    """
    我去建表  但测试的话 要把main那里的Q161->Q163,MIDF ->MIDJ
    因为161没有跑Z_A的
    """
    # data type是 0 D M。分别是 实时、每日、每月
    # p_data_type = 'D'
    # p_unit = 'Q112'
    # p_account = '1001'
    # p_cost_center = 'MICA'
    # p_account_period_start = '20210423220000'
    # p_account_period_end = '20210424220000'

    UNINFOJob(p_config=p_config,
              p_db_conn_mpp=p_db_conn_mpp,
              p_db_conn_rds=p_db_conn_rds,
              p_db_conn_dbprod7=p_db_conn_dbprod7,
              p_account_period_start=p_account_period_start,
              p_account_period_end=p_account_period_end).execute()

def run_dp0101_job(p_config=None,
                   p_db_conn_mpp=None,
                   p_db_conn_rds=None,
                   p_db_conn_dbprod7=None,
                   p_data_type=None,
                   p_unit=None,
                   p_account=None,
                   p_cost_center=None,
                   p_account_period_start=None,
                   p_account_period_end=None):
    """
    我去建表  但测试的话 要把main那里的Q161->Q163,MIDF ->MIDJ
    因为161没有跑Z_A的
    """
    # data type是 0 D M。分别是 实时、每日、每月
    # p_data_type = 'D'
    # p_unit = 'Q112'
    # p_account = '1001'
    # p_cost_center = 'MICA'
    # p_account_period_start = '20210423220000'
    # p_account_period_end = '20210424220000'

    DP0101Job(p_config=p_config,
              p_db_conn_mpp=p_db_conn_mpp,
              p_db_conn_rds=p_db_conn_rds,
              p_db_conn_dbprod7=p_db_conn_dbprod7,
              p_unit=p_unit,
              p_account=p_account,
              p_cost_center=p_cost_center,
              p_account_period_start=p_account_period_start,
              p_account_period_end=p_account_period_end,
              p_data_type=p_data_type).execute()


def run_dp0102_job(p_config=None,
                   p_db_conn_mpp=None,
                   p_db_conn_rds=None,
                   p_db_conn_dbprod7=None,
                   p_data_type=None,
                   p_unit=None,
                   p_account=None,
                   p_cost_center=None,
                   p_account_period_start=None,
                   p_account_period_end=None):
    # p_data_type = 'D'
    # p_unit = 'Q112'
    # p_account = '1001'
    # p_cost_center = 'MICA'
    # p_account_period_start = '20210423220000'
    # p_account_period_end = '20210424220000'

    code = DP0102Job(p_config=p_config,
                     p_db_conn_mpp=p_db_conn_mpp,
                     p_db_conn_rds=p_db_conn_rds,
                     p_db_conn_dbprod7=p_db_conn_dbprod7,
                     p_unit=p_unit,
                     p_data_type=p_data_type,
                     p_account=p_account,
                     p_cost_center=p_cost_center,
                     p_account_period_start=p_account_period_start,
                     p_account_period_end=p_account_period_end).execute()
    print(code)


def run_jhzkey_job(p_config=None,
                   p_db_conn_mpp=None,
                   p_db_conn_rds=None,
                   p_db_conn_dbprod7=None,
                   p_data_type=None,
                   p_unit=None,
                   p_account=None,
                   p_cost_center=None,
                   p_account_period_start=None,
                   p_account_period_end=None):
    # p_data_type = 'D'
    # p_unit = 'Q112'# Q161 ?????? Q314
    # p_account = '1001'
    # p_cost_center = 'MICA' # MIDF ?????????? MIHF
    # p_account_period_start = '20210423220000'
    # p_account_period_end = '20210424220000'

    JHZKEYJob(p_config=p_config,
              p_db_conn_mpp=p_db_conn_mpp,
              p_db_conn_rds=p_db_conn_rds,
              p_db_conn_dbprod7=p_db_conn_dbprod7,
              p_unit=p_unit,
              p_account=p_account,
              p_cost_center=p_cost_center,
              p_account_period_start=p_account_period_start,
              p_account_period_end=p_account_period_end,
              p_data_type=p_data_type).execute()


def run_ca0101_job(p_config=None,
                   p_db_conn_mpp=None,
                   p_db_conn_rds=None,
                   p_db_conn_dbprod7=None,
                   p_data_type=None,
                   p_unit=None,
                   p_account=None,
                   p_cost_center=None,
                   p_account_period_start=None,
                   p_account_period_end=None):
    # p_data_type = 'D'
    # p_unit = 'Q112'
    # p_account = '1001'
    # p_cost_center = 'MICA'
    # p_account_period_start = '20210423220000'
    # p_account_period_end = '20210424220000'

    CA0101Job(p_config=p_config,
              p_db_conn_mpp=p_db_conn_mpp,
              p_db_conn_rds=p_db_conn_rds,
              p_db_conn_dbprod7=p_db_conn_dbprod7,
              p_unit=p_unit,
              p_account=p_account,
              p_cost_center=p_cost_center,
              p_account_period_start=p_account_period_start,
              p_account_period_end=p_account_period_end,
              p_data_type=p_data_type).execute()


if __name__ == '__main__':
    start = datetime.datetime.now()

    status = main()

    elapsed = float((datetime.datetime.now() - start).seconds)
    print("Time Used 4 All ----->>>> %f seconds" % (elapsed))